package cn.dujc.superkid.activities;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;


import cn.dujc.core.adapter.BaseQuickAdapter;
import cn.dujc.core.ui.BaseListActivity;
import cn.dujc.core.util.TextColorBuilder;
import cn.dujc.core.util.ToastUtil;
import cn.dujc.superkid.R;
import cn.dujc.superkid.adapter.CourseCatalogueAdapter;
import cn.dujc.superkid.adapter.MatchDetailAdapter;
import cn.dujc.superkid.api.HttpHelper;
import cn.dujc.superkid.api.QMap;
import cn.dujc.superkid.api.Result;
import cn.dujc.superkid.api.ResultCallBack;
import cn.dujc.superkid.bean.AnswerListResult;
import cn.dujc.superkid.bean.CupListResult;
import cn.dujc.superkid.bean.RegisterResult;
import cn.dujc.superkid.bean.VideoListResult;
import cn.dujc.superkid.utils.Preferences;


public class MatchDetailActivity extends BaseListActivity{
    private int examineid;
    private final List<Object> mList = new ArrayList<>();
    private List<CupListResult.VideoBean> videoList;
    private MatchDetailAdapter matchDetailAdapter;
    private View mHeaderView;
    private TextView mTvTitle;
    private TextView mTvDesc;
    private TextView mTvInfo;
    private int cup_id;
    private String cupId;
    private String title;
    private TextView toolbar_title_id;
    @Nullable
    @Override
    protected BaseQuickAdapter initAdapter() {
        matchDetailAdapter = new MatchDetailAdapter(mList);
        matchDetailAdapter.addHeaderView(initHeader(null));
        return matchDetailAdapter;
    }

    @Override
    public void initBasic(Bundle savedInstanceState) {
        Intent intent = getIntent();
        cupId = intent.getStringExtra("course_id");
        if (cupId !=null){
            cup_id = Integer.parseInt(cupId);
        }
        super.initBasic(savedInstanceState);
    }

    @Nullable
    @Override
    public Toolbar initToolbar(ViewGroup parent) {
        final Toolbar toolbar = (Toolbar) LayoutInflater.from(parent.getContext()).inflate(R.layout.toolbar_matchdetail, parent, false);
        toolbar.findViewById(R.id.toolbar_menu_id);
        toolbar_title_id= toolbar.findViewById(R.id.toolbar_title);
        toolbar.findViewById(R.id.toolbar_back_id).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        Intent intent = getIntent();
        title= intent.getStringExtra("title");
        toolbar_title_id.setText(title);

        return toolbar;
    }

    private View initHeader(CupListResult cupListResult) {
        if (mHeaderView == null) {
            mHeaderView = getLayoutInflater().inflate(R.layout.header_course_detail, null);
            mTvTitle = mHeaderView.findViewById(R.id.tv_title);
            mTvDesc = mHeaderView.findViewById(R.id.tv_desc);
            mTvInfo = mHeaderView.findViewById(R.id.tv_info);
//            mHeaderView.findViewById(R.id.toolbar_back_id).setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    finish();
//                }
//            });
        }
        if (cupListResult != null) {
            mTvTitle.setText(cupListResult.getTitle());
            mTvDesc.setText(cupListResult.getIntroduce());
            int color = ContextCompat.getColor(mActivity, R.color.colorAccent);
            mTvInfo.setText(new TextColorBuilder()
                    .addTextPart("累计")
                    .addTextPart(color, cupListResult.getStudy_num()+"")
                    .addTextPart("位同学参加了学习，课程总共")
                    .addTextPart(color, cupListResult.getCourse_num()+"")
                    .addTextPart("节")
                    .build());
        }
        return mHeaderView;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (Preferences.loginSuccess(requestCode, resultCode)) {
            reload();
        }
    }

    @Override
    protected void onItemClick(int position) {
        final Object item = matchDetailAdapter.getItem(position);
        if (item instanceof CupListResult.VideoBean) {//视频
            starter()
                    .with("video_id",videoList.get(position-1).getId()+"")
                    .with("course_id",cup_id+"")
                    .with("is_learn",videoList.get(position).getIs_learn())
                    .with("type","3")
                    .with("videoTitle",videoList.get(position).getVideo_title())
                    .go(CupVideoActivity.class);
        } else if (item instanceof CupListResult.ExamineListBean) {//考试
            starter().with("examine_id",((CupListResult.ExamineListBean)item).getId()+"").go(CupExplainActivity.class);
        }
    }

    @Override
    protected void loadMore() {
        refreshDone();
        loadDone(true, true);
    }

    @Override
    protected void reload() {
        loadData();

    }

    private void loadData() {
        showRefreshing();


        final RegisterResult loginResult = Preferences.getInstance().getLoginResult(mActivity);
        HttpHelper.getCupMatch().cupList(QMap.build()
                .put("user_id", loginResult.getUid())
                .put("channel", Preferences.getInstance().getChannel(mActivity).getChannel_id())
                .put("cup_id", cup_id)
                .put("logincode", loginResult.getLogincode())
                .create()).enqueue(new ResultCallBack<Result<CupListResult>>(mActivity) {
            @Override
            public void onSuccessCallback(int code, Result<CupListResult> body) {
                if (body != null){
                    final CupListResult data = body.getData();
                    if (data != null) {
                        initHeader(data);
                        videoList = data.getVideo();
                        if (videoList != null && videoList.size() > 0) {
                            mList.add("课程目录");
                            mList.addAll(videoList);
                        }

                        final List<CupListResult.ExamineListBean> examine_list = data.getExamine_list();
                        if (examine_list != null && examine_list.size() > 0) {
                            mList.add("测试卷");
                            mList.addAll(examine_list);
                        }
                    }
                }
                notifyDataSetChanged(true);
            }
        });
    }
}
